import {Base64} from 'js-base64'

Page({
    // 获取token
    onGetToken() {
        // wx.login：获取当前用户的code
        wx.login({
            success: (res) => {
                if (res.code) {
                    wx.request({
                        url: 'http://localhost:3333/v1/token',
                        method: 'POST',
                        data: {
                            // 帐号
                            account: res.code,
                            type: 100
                        },
                        success: (res) => {
                            console.log(res.data)
                            const code = res.statusCode.toString()
                            // 请求成功
                            if (code.startsWith('2')) {
                                wx.setStorageSync('token', res.data.token)
                            }
                        }
                    })
                }
            }
        })
    },

    // 校验token
    onVerifyToken() {
        wx.request({
            url: 'http://localhost:3333/v1/token/verify',
            method: 'POST',
            data: {
                token: wx.getStorageSync('token')
                // token: '123456'
            },
            success: res => {
                console.log('res', res)
            }
        })
    },

    // 获取下一期期刊
    onGetNext() {
        wx.request({
            url: 'http://localhost:3333/v1/classic/6/next',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取上一期期刊
    onGetPrevious() {
        wx.request({
            url: 'http://localhost:3333/v1/classic/6/previous',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取最新期刊
    onGetLatest() {
        wx.request({
            url: 'http://localhost:3333/v1/classic/latest',
            method: 'GET',
            success: res => {
                console.log('res', res)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取期刊点赞情况
    onGetClassicFavor() {
        wx.request({
            url: 'http://localhost:3333/v1/classic/100/1/favor',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取我喜欢的期刊
    onGetMyFavorList() {
        wx.request({
            url: 'http://localhost:3333/v1/classic/favor',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取期刊详情
    onGetClassicDetail() {
        wx.request({
            url: 'http://localhost:3333/v1/classic/200/2',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 点赞
    onLike() {
        wx.request({
            url: 'http://localhost:3333/v1/like',
            method: 'POST',
            data: {
                art_id: 1,
                type: 100
            },
            success: res => {
                console.log('res', res)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 取消点赞
    onDisLike() {
        wx.request({
            url: 'http://localhost:3333/v1/like/cancel',
            method: 'POST',
            data: {
                art_id: 1,
                type: 100
            },
            success: res => {
                console.log('res', res)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // base64加密
    _encode() {
        const token = wx.getStorageSync('token')
        const base64 = Base64.encode(token + ':')
        return 'Basic ' + base64
    },

    // 获取热门书籍列表
    onGetHotBookList() {
        wx.request({
            url: 'http://localhost:3333/v1/book/hot_list',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取书籍详情
    onGetBookDetail() {
        wx.request({
            url: 'http://localhost:3333/v1/book/1120/detail',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 搜索书籍
    onBookSearch() {
        wx.request({
            url: 'http://localhost:3333/v1/book/search',
            method: 'GET',
            data: {
                q: '韩寒',
                count: 5
            },
            // like key%
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取我喜欢书籍的数量
    onGetMyFavorsBookCount(){
        wx.request({
            url: 'http://localhost:3333/v1/book/favor/count',
            method: 'GET',
            // like key%
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取书籍点赞情况
    onGetBookFavor() {
        wx.request({
            url: 'http://localhost:3333/v1/book/1120/favor',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 新增短评
    onAddShortComment() {
        wx.request({
            url: 'http://localhost:3333/v1/book/add/short_comment',
            method: 'POST',
            data: {
                content:'春风十里不如有你',
                book_id:1120
            },
            // like key%
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

    // 获取书籍短评
    onGetComments() {
        wx.request({
            url: 'http://localhost:3333/v1/book/1120/short_comment',
            method: 'GET',
            success: res => {
                console.log(res.data)
            },
            header: {
                Authorization: this._encode()
            }
        })
    },

})